package com.liliya.servlet.ad;

import com.liliya.pojo.Ad;
import com.liliya.service.impl.AdServiceImpl;
import com.liliya.utils.RandomFile;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/13 14:23
 */
@WebServlet(urlPatterns = "/ad/show")
public class AdShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RandomFile randomFile = new RandomFile();
        String fileName = randomFile.getFileName("D:\\JavaProject\\jsp\\shopjsp\\out\\artifacts\\shopjsp_war_exploded\\upload\\img");
        System.out.println(fileName);
        AdServiceImpl adService = new AdServiceImpl();
        String adnum = req.getParameter("adnum");
        if (adnum != null) {
            Ad ad = adService.findAdByAdNum(Integer.parseInt(adnum));
            fileName = ad.getAdlink();
        }else fileName = "\\upload\\img\\"+fileName;


        req.setAttribute("fileName",fileName);
        req.getRequestDispatcher("./adshow.jsp").forward(req,resp);

    }
}
